package org.wddsicmd.util;

import org.wddsicmd.util.Log;

import java.lang.Object;

import java.io.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import java.util.ArrayList;
import java.util.List;

import java.awt.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.Timer;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeModelListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;
public class CustomProperties {

	private final static TAG = "CustomProperties";

	String mConfigPropertiesFile;

	public CustomProperties(){
		mConfigPropertiesFile = getConfigPropertiesFile();
	}

	public  String getValue(String key) {
	 Properties props = new Properties();
		   try {
			InputStream in = new BufferedInputStream (new FileInputStream(mConfigPropertiesFile));
			props.load(in);
			String value = props.getProperty (key);
			   //System.out.println(key+value);
			   return value;
		   } catch (Exception e) {
			e.printStackTrace();
			return null;
		   }
	}

	   public  void setValue(String parameterName,String parameterValue) {
		Properties prop = new Properties();
		try {
		 InputStream fis = new FileInputStream(mConfigPropertiesFile);
			   prop.load(fis);
			   OutputStream fos = new FileOutputStream(mConfigPropertiesFile);
			   prop.setProperty(parameterName, parameterValue);
			   prop.store(fos, "Update '" + parameterName + "' value");
			   Log.d(TAG, "setValue parameterName:"+parameterName+" parameterValue:"+parameterValue);
		   } catch (IOException e) {
			System.err.println("Visit "+mConfigPropertiesFile+" for updating "+parameterName+" value error");
		   }
	   }

	public static String readValue(String filePath,String key) {
	 Properties props = new Properties();
		   try {
			InputStream in = new BufferedInputStream (new FileInputStream(filePath));
			props.load(in);
			String value = props.getProperty (key);
			   //System.out.println(key+value);
			   return value;
		   } catch (Exception e) {
			e.printStackTrace();
			return null;
		   }
	}
	
	   public static void readProperties(String filePath) {
		Properties props = new Properties();
		   try {
			InputStream in = new BufferedInputStream (new FileInputStream(filePath));
			props.load(in);
			   Enumeration en = props.propertyNames();
				while (en.hasMoreElements()) {
				 String key = (String) en.nextElement();
					   String Property = props.getProperty (key);
					   //System.out.println(key+Property);
				   }
		   } catch (Exception e) {
			e.printStackTrace();
		   }
	   }
	
	   public static void writeProperties(String filePath,String parameterName,String parameterValue) {
		Properties prop = new Properties();
		try {
		 InputStream fis = new FileInputStream(filePath);
			   prop.load(fis);
			   OutputStream fos = new FileOutputStream(filePath);
			   prop.setProperty(parameterName, parameterValue);
			   prop.store(fos, "Update '" + parameterName + "' value");
		   } catch (IOException e) {
			System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
		   }
	   }

       private String getConfigPropertiesFile(){
	   	return "/msscfg.properties";
/* doom-2015-10-29 10:54:10 
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/MSS/cfg.properties";
		}else{
			return "C:\\MSS\\cfg.properties";
		}
 doom-2015-10-29 10:54:10 */
       }


}

